﻿using System;
using System.Collections.Generic;
using System.Text;
using Peekaboo2008.model;
using System.Data;
using Peekaboo2008.util;
using System.Data.SqlClient;

namespace Peekaboo2008.dao
{
    class KhuVucDAO
    {
        public static DataTable getAll()
        {
            return DataUtil.executeStore("sp_KhuVuc_GetAll", null);
        }

        public static DataTable getAllOrderByName()
        {
            return DataUtil.executeStore("sp_KhuVuc_GetAllOrderByName", null);
        }
        public static Boolean insert(KhuVucModel _model)
        {
            SqlParameter[] param = new SqlParameter[2];
            int i = 0;
            param[i] = new SqlParameter("Ten", SqlDbType.NVarChar);
            param[i++].Value = _model.Ten;
            param[i] = new SqlParameter("Ghi_chu", SqlDbType.VarChar);
            param[i++].Value = _model.GhiChu;

            return DataUtil.executeNonStore("sp_KhuVuc_Insert", param);
        }

        public static Boolean update(KhuVucModel _model)
        {
            SqlParameter[] param = new SqlParameter[3];
            int i = 0;
            param[i] = new SqlParameter("ID", SqlDbType.Int);
            param[i++].Value = _model.MaKhuVuc;
            param[i] = new SqlParameter("Ten", SqlDbType.NVarChar);
            param[i++].Value = _model.Ten;
            param[i] = new SqlParameter("Ghi_chu", SqlDbType.VarChar);
            param[i++].Value = _model.GhiChu;

            return DataUtil.executeNonStore("sp_KhuVuc_Update", param);
        }

        public static KhuVucModel getKhuVucByID(String maKhuVuc)
        {
            KhuVucModel khuVucModel = null;

            SqlParameter[] param = new SqlParameter[1];
            int i = 0;
            param[i] = new SqlParameter("id", SqlDbType.NVarChar);
            param[i++].Value = maKhuVuc;
            DataTable kq = DataUtil.executeStore("sp_KhuVuc_GetKhuVucByID", param);
            foreach (DataRow row in kq.Rows)
            {
                khuVucModel = new KhuVucModel();
                khuVucModel.MaKhuVuc = row["ID"].ToString();
                khuVucModel.Ten = row["Ten"].ToString();
                khuVucModel.GhiChu = row["Ghi_chu"].ToString();
                khuVucModel.NgayCapNhatCuoi = row["Ngay_cap_nhat_cuoi"].ToString();
            }
            return khuVucModel;
        }
    }
}
